<template>
  <div style="background-color: white;height: calc(100vh - 30px);">
    <tiny-split v-model="split1">
      <template #left>
        <tiny-divider>兑付条件筛选</tiny-divider>
        <div style="margin-top: 20px;padding: 0 10px;">
          <x-form v-model:api="fApi" v-model="searchModel" :rules="cons" ></x-form>
        </div>
        <div style="padding: 10px;">
          <tiny-checkbox-group v-model="sendVal">
            <tiny-checkbox v-for="city in sends" :label="city" :key="city">
              {{ city }}
            </tiny-checkbox>
          </tiny-checkbox-group>
          <div style="margin-top: 20px;text-align: center;">
            <JButton type="info" @click="calc">计算兑付明细</JButton>
          </div>
        </div>
        <tiny-divider>兑付主体</tiny-divider>
        <div style="padding: 0 10px;">
          <x-form v-model:api="fApi1" v-model="searchModel1" :rules="cons1"></x-form>
          <div style="margin-top: 10px;padding-left: 10px;">
            <tiny-radio v-model="dfType" label="转预付"></tiny-radio>
            <tiny-radio v-model="dfType" label="转其他收入"></tiny-radio>
          </div>
          <div v-if="dfType==='转其他收入'" style="padding: 10px;">
            <JAuto v-model="accName" @change="changeAcc" cat="accountList" placeholder="收入账户" />
          </div>
          <tiny-divider>操作</tiny-divider>
          <div style="text-align: center;padding: 10px;">
            <j-space>
              <JButton @click="reset">重置</JButton>
              <JButton type="info" @click="save" :loading="loading">保存并审核</JButton>
            </j-space>
          </div>
        </div>

      </template>
      <template #right>
        <div style="padding: 10px;text-align: right;">
          <JButton @click="hist">兑付历史记录</JButton>
        </div>
        <tiny-divider>兑付商品汇总</tiny-divider>
        <div style="padding: 10px;">
          <j-grid v-model="list" :columns="fieldsDetail" />
        </div>
      </template>
    </tiny-split>
    <zcdflist ref="listRef"></zcdflist>
  </div>
</template>
<script>
// 政策兑付
import tool from "@/utils/tool";
import Zcdflist from "@/views/fina/zcdflist.vue";
export default {
  name: "zcdf",
  components:{ Zcdflist},
  data () {
    return {
      split1: 0.2,
      show: false,
      fApi1: {},
      fApi: {},
      searchModel1:{
        buId:'',
        buName: '',
        billDate: tool.getDay(),
      },
      searchModel:{
        endDate: '',
        brandId: '',
        brandName:'',
        invId: '',
        invName: ''
      },
      cons1: [
        {field: 'buId',hide: true},
        {field: 'billDate',title: '兑付日期',com: 'JPicker', required: true,},
        {field: 'buName', title:'供应商',com: 'JAuto', cat: 'provider' ,required: true,idField:'buId'},
      ],
      cons: [
        {field: 'brandId',hide: true},
        {field: 'invId',hide: true},
        {field: 'endDate', title:'截止日期',com: 'JPicker',required: true },
        {field: 'brandName', title:'品牌',com: 'JAuto', cat: 'brand',idField:'brandId'},
        {field: 'invName', title:'商品',com: 'JAuto', cat: 'goods',idField: 'invId' },
      ],
      conditions:{
        endDate: tool.getDay(),
        buId:'',
        buName: '',
        brandId: '',
        brandName:'',
        invId: '',
        invName: ''
      },
      list: [],
      sendVal: [],
      sends:[],
      dfType: '',
      id: '',
      accId: '',
      accName: '',
      loading: false
    }
  },
  created() {
    tool.post('common/dropdown',{cat: 'send'}).then(ret => {
      ret.data.map(row => {
        this.sends.push(row.name)
      })
    })
  },
  computed: {
    fieldsDetail () {
      let f = [
        {type: 'index', width: 60,fixed: 'left'},
        {title: '商品名称',width: 200, field: 'invName',fixed: 'left',align: 'left'},
        {title: '品牌',width: 80, field: 'brandName'},
        {title: '规格',width: 70, field: 'spec'},
        {title: '单位',width: 70, field: 'unit'},
        {title: '兑付单价',width: 80, field: 'sendPrice',formatter:'formatMoney',align: 'right'},
        {title: '代赠',width: 80, field: 'dz',formatter:'formatMoney',align: 'right',sum: true},
        {title: '自赠',width: 80, field: 'zz',formatter:'formatMoney',align: 'right',sum: true},
        {title: '陈列',width: 80, field: 'cl',formatter:'formatMoney',align: 'right',sum: true},
        {title: '销量',width: 80, field: 'xl',formatter:'formatMoney',align: 'right',sum: true},
        {title: '合同',width: 80, field: 'ht',formatter:'formatMoney',align: 'right',sum: true},
        {title: '开业',width: 80, field: 'ky',formatter:'formatMoney',align: 'right',sum: true},
        {title: '赠饮',width: 80, field: 'zy',formatter:'formatMoney',align: 'right',sum: true},
        {title: '合计数量',width: 80, field: 'qty',formatter:'formatMoney',align: 'right',sum: true},
        {title: '兑付金额',width: 100, field: 'amount',formatter:'formatMoney',align: 'right',sum: true},
      ]
      return f
    }
  },
  methods: {
    add () {
      this.show = true
    },
    getList () {
      tool.post('pay/zcdf',{formData: this.searchModel}).then(ret => {
        this.list = ret.data.list
      })
    },
    calc () {
      if(!this.searchModel.endDate) { return tool.err('请选择截止日期') }
      if(this.sendVal.length === 0) return tool.err('请选择赠品类型')
      let model = Object.assign(this.searchModel, {sendType: this.sendVal})
      model.id = this.id
      this.list = []
      tool.post('pay/calcDf', {formData: model}).then(ret => {
        this.list = ret.data.list
        this.id = ret.data.id
      })
    },
    changeAcc (val) {
      this.accId = val?val.id:''
    },
    reset () {
      this.id = ''
      this.list = []
      this.sendVal = []
      this.searchModel.endDate = ''
      this.searchModel.brandId = ''
      this.searchModel.brandName = ''
      this.searchModel.invId = ''
      this.searchModel.invName = ''
      this.dfType = ''
      this.searchModel1.buId = ''
      this.searchModel1.buName = ''
      this.searchModel1.billDate = tool.getDay()
    },
    save () {
      if(!this.searchModel1.buId) { return tool.err('请选择供应商') }
      if(!this.searchModel1.billDate) { return tool.err('兑付日期不能为空') }
      if(!this.id) return tool.err('还没有计算数据')
      if(!this.dfType) return tool.err('还没有选择兑付类型')
      this.loading = true
      setTimeout(() => {
        this.loading = false
      }, 2000)
      tool.post('bill/saveDf',{
        id: this.id,
        dfType: this.dfType,
        accId: this.accId,
        accName: this.accName,
        buId: this.searchModel1.buId,
        buName: this.searchModel1.buName,
        billDate: this.searchModel1.billDate,
      }).then(ret => {
        if(ret.stat) {
          this.reset()
          this.loading = false
        }
      })
    },
    hist () {
      this.$refs.listRef.open()
    },
  }
};
</script>

<style scoped lang="scss">

</style>